package com.nd.qa.shareplate.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import com.alibaba.fastjson.JSONObject;

public class HttpUrlUtil {
	
	private static  URL url ;

	public String connectionUrl(String param,String path){
		String result = "";
		try {
			url = new URL(path);  
			HttpURLConnection httpURLConnection = (HttpURLConnection) url  
	                .openConnection();  
	        httpURLConnection.setConnectTimeout(3000);  
	        httpURLConnection.setDoInput(true);// 从服务器获取数据  
	        httpURLConnection.setDoOutput(true);// 向服务器写入数据  
	        // 获得上传信息的字节大小及长度  
	        byte[] mydata = param.getBytes();  
	        // 设置请求体的类型  
	        httpURLConnection.setRequestProperty("Content-Type",  
	                "application/json");  
	        httpURLConnection.setRequestProperty("charset", "utf-8");
	        httpURLConnection.setRequestMethod("POST");
	        httpURLConnection.setRequestProperty("Content-Lenth",  
	        		String.valueOf(mydata.length));  
	        
	        // 获得输出流，向服务器输出数据  
	        OutputStream outputStream = (OutputStream) httpURLConnection  
	                .getOutputStream();  
	        outputStream.write(mydata);  

	        // 获得服务器响应的结果和状态码  
	        int responseCode = httpURLConnection.getResponseCode();
	        if (200==responseCode) {
	        	InputStream inputStream = (InputStream) httpURLConnection  
                        .getInputStream();
	        	result= (changeInputStream(inputStream, "GBK"));//本机 utf-8   服务器GBK
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return result;
	}
	
	public static String changeInputStream(InputStream inputStream, String encode) {  
		  
        // ByteArrayOutputStream 一般叫做内存流  
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
        byte[] data = new byte[1024];  
        int len = 0;  
        String result = "";  
        if (inputStream != null) {  
            try {  
                while ((len = inputStream.read(data)) != -1) {  
                    byteArrayOutputStream.write(data, 0, len);  
                }  
                result = new String(byteArrayOutputStream.toByteArray(), encode);  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
        return result;  
    }  
	public static void main(String[] argss) throws IOException {
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("userName", "421490");
		jsonObject.put("userPassword", "1");
		HttpURLConnection httpURLConnection = (HttpURLConnection) url  
                .openConnection();  
        httpURLConnection.setConnectTimeout(3000);  
        httpURLConnection.setDoInput(true);// 从服务器获取数据  
        httpURLConnection.setDoOutput(true);// 向服务器写入数据  
        // 获得上传信息的字节大小及长度  
        byte[] mydata = jsonObject.toString().getBytes();  
        // 设置请求体的类型  
        httpURLConnection.setRequestProperty("Content-Type",  
                "application/json");  
        httpURLConnection.setRequestProperty("charset", "utf-8");
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Lenth",  
        		String.valueOf(mydata.length));  
        
        // 获得输出流，向服务器输出数据  
        OutputStream outputStream = (OutputStream) httpURLConnection  
                .getOutputStream();  
        outputStream.write(mydata);  

        // 获得服务器响应的结果和状态码  
        int responseCode = httpURLConnection.getResponseCode(); 
	}
}
